package cn.iocoder.yudao.module.worker.controller.admin.clerk;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerklevel.ClerkLevelDO;
import cn.iocoder.yudao.module.worker.service.clerklevel.ClerkLevelService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;

import cn.iocoder.yudao.module.worker.controller.admin.clerk.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerk.ClerkDO;
import cn.iocoder.yudao.module.worker.service.clerk.ClerkService;

@Tag(name = "管理后台 - 店员信息")
@RestController
@RequestMapping("/worker/clerk")
@Validated
public class ClerkController {

    @Resource
    private ClerkService clerkService;

    @Resource
    private ClerkLevelService clerkLevelService;

    @PostMapping("/create")
    @Operation(summary = "创建店员信息")
    @PreAuthorize("@ss.hasPermission('worker:clerk:create')")
    public CommonResult<Long> createClerk(@Valid @RequestBody ClerkSaveReqVO createReqVO) {
        return success(clerkService.createClerk(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店员信息")
    @PreAuthorize("@ss.hasPermission('worker:clerk:update')")
    public CommonResult<Boolean> updateClerk(@Valid @RequestBody ClerkSaveReqVO updateReqVO) {
        clerkService.updateClerk(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-onlineStatus")
    @Operation(summary = "修改店员在线状态")
    @PreAuthorize("@ss.hasPermission('worker:clerk:update')")
    public CommonResult<Boolean> updateClerkOnlineStatus(@Valid @RequestBody ClerkSaveReqVO reqVO) {
        clerkService.updateClerkOnlineStatus(reqVO.getId(), reqVO.getOnlineStatus());
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改店员状态")
    @PreAuthorize("@ss.hasPermission('worker:clerk:update')")
    public CommonResult<Boolean> updateClerkStatus(@Valid @RequestBody ClerkSaveReqVO reqVO) {
        clerkService.updateClerkStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @PutMapping("/update-blindStatus")
    @Operation(summary = "修改店员盲盒状态")
    @PreAuthorize("@ss.hasPermission('worker:clerk:update')")
    public CommonResult<Boolean> updateClerkBlindStatus(@Valid @RequestBody ClerkSaveReqVO reqVO) {
        clerkService.updateClerkBlindStatus(reqVO.getId(), reqVO.getBlindStatus());
        return success(true);
    }

    @PutMapping("/update-hot")
    @Operation(summary = "修改店员人气店员")
    @PreAuthorize("@ss.hasPermission('worker:clerk:update')")
    public CommonResult<Boolean> updateClerkHot(@Valid @RequestBody ClerkSaveReqVO reqVO) {
        clerkService.updateClerkHot(reqVO.getId(), reqVO.getHot());
        return success(true);
    }

    @PutMapping("/update-home")
    @Operation(summary = "修改店员人首页推荐")
    @PreAuthorize("@ss.hasPermission('worker:clerk:update')")
    public CommonResult<Boolean> updateClerkHome(@Valid @RequestBody ClerkSaveReqVO reqVO) {
        clerkService.updateClerkHome(reqVO.getId(), reqVO.getHome());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店员信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('worker:clerk:delete')")
    public CommonResult<Boolean> deleteClerk(@RequestParam("id") Long id) {
        clerkService.deleteClerk(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店员信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('worker:clerk:query')")
    public CommonResult<ClerkRespVO> getClerk(@RequestParam("id") Long id) {
        ClerkDO clerk = clerkService.getClerk(id);
        return success(BeanUtils.toBean(clerk, ClerkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店员信息分页")
    @PreAuthorize("@ss.hasPermission('worker:clerk:query')")
    public CommonResult<PageResult<ClerkRespVO>> getClerkPage(@Valid ClerkPageReqVO pageReqVO) {
        PageResult<ClerkDO> pageResult = clerkService.getClerkPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty());
        }
        List<ClerkLevelDO> clerkLevelList = clerkLevelService.getClerkLevelListByIds(convertList(pageResult.getList(), ClerkDO::getWorkerClerkLevelId));
        Map<Long, ClerkLevelDO> clerkLevelMap = convertMap(clerkLevelList, ClerkLevelDO::getId);
        return success(BeanUtils.toBean(pageResult, ClerkRespVO.class,
                clerkRespVO ->
                        clerkRespVO.setClerkLevel(clerkLevelMap.getOrDefault(clerkRespVO.getWorkerClerkLevelId(), new ClerkLevelDO()).getName())));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店员信息 Excel")
    @PreAuthorize("@ss.hasPermission('worker:clerk:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClerkExcel(@Valid ClerkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClerkDO> list = clerkService.getClerkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店员信息.xls", "数据", ClerkRespVO.class,
                        BeanUtils.toBean(list, ClerkRespVO.class));
    }

}